import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import './assets/css/global.css'

import { get, post } from './utils/ajax'
import VuePageTransition from 'vue-page-transition'
import {printLog, updateWindowTitle} from './utils/downloadImg'
import 'nprogress/nprogress.css'
import NProgress from 'nprogress'

// ----------------自定义引入 start-------------
import {
  Pagination,
  Input,
  Button,
  Popover,
  Form,
  FormItem,
  Icon,
  Image,
  Loading,
  Message,
  Progress
} from 'element-ui'

Vue.config.productionTip = false
Vue.use(VuePageTransition)

Vue.use(Pagination)
Vue.use(Input)
Vue.use(Button)
Vue.use(Popover)
Vue.use(Form)
Vue.use(FormItem)
Vue.use(Icon)
Vue.use(Image)
Vue.use(Loading)
Vue.use(Progress)

Vue.use(Loading.directive)
Vue.prototype.$loading = Loading.service
Vue.prototype.$message = Message
// ----------------自定义引入 End-------------

Vue.prototype.$get = get
Vue.prototype.$post = post
Vue.config.productionTip = false

NProgress.configure({showSpinner: false})

router.afterEach((to, from) => {
  store.dispatch('updatePageName', to.name)
})

window.tellWebProgress = (progress) => {
  store.dispatch('updateDownloadImgProgress', progress)
  printLog('===' + progress + '===')
}

window.tellWebDownloadOK = () => {
  Message.success('恭喜, 已为您设置好当前桌面壁纸')
  window.setTimeout(() => {
    store.dispatch('updateDownloadImgProgress', -1)
    printLog('===Done===')
  }, 2000)
}

window.tellWebDownloadFailed = () => {
  store.dispatch('updateDownloadImgProgress', -1)
  Message.success('设置桌面壁纸失败了')
}

router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})
router.afterEach((to, from) => {
  NProgress.done()
  console.log('To: ', to.meta.title)
  if (to.meta && to.meta.title) {
    window.document.title = to.meta.title + ' | 桌面壁纸'
  } else {
    window.document.title = 'DI桌面壁纸'
  }
  updateWindowTitle(window.document.title)
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
